<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            线边配送确认
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">配送任务号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="distributeTaskCode" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">目标仓库</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="workCenterCode" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调入库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="workCellCode" value="" disabled="true" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">扫码确认</div>
                    <div class="aui-list-item-inner">
                        <input type="text" class="aui-input" id="boxnum" placeholder="请扫描箱码" value="" />
                        <i class="aui-input-addon aui-iconfont aui-icon-scan "
                            style="padding-right:10px;font-size: 1.6rem" onclick="doScan();">
                        </i>
                    </div>
                </div>
            </li>
        </ul>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'>序号</td>
                        <td algin='center'>物料编码</td>
                        <td algin='center'>规格</td>
                        <td algin='center'>物料名称</td>
                        <td algin='center'>要货数量</td>
                        <td algin='center'>配送数量</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
        <div>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="margin-left:5%;width:100%">总箱数：</div>
                        <div class="aui-list-item-input" style="margin-left:5%">
                            <input type="text" id="num" value="" disabled="true" style='color:red' />
                        </div>
                        <div class="aui-list-item-label" style="margin-left:5%;width:100%">已确认箱数：</div>
                        <div class="aui-list-item-input" style="margin-left:5%">
                            <input type="text" id="mrlnum" value="" disabled="true" style='color:red' />
                        </div>
                    </div>
                </li>
            </ul>
        </div>


        <ul class="ui-selectee" id="tbodyHtml2">

        </ul>
        <!-- <div style="width:100%;position: fixed;top: 0px;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSHconfig()">一键确认收货</div>
        <div style="padding-top:3%;"></div> -->
    </div>
    <div style="height:60px;"></div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSHconfig()">
            <i class="aui-iconfont  aui-text-epblue" style="font-size: 0.9rem">一键确认收货</i>
        </div>
    </footer>

    <!-- <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div> -->

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var distributeTaskCode = ""; //配送任务号
    var workCenterCode = ""; //仓库编码
    var workCellCode = ""; //库位编码
    var boxnum = ""; // 箱码
    var arrSave = [];
    var num = 0;

    var scanRule = ""; //条码解析规则
    var arrList = [];

    //初始化
    apiready = function () {
        distributeTaskCode = api.pageParam.distributeTaskCode;//配送任务号
        $("#distributeTaskCode").val(distributeTaskCode);
        workCenterCode = api.pageParam.workCenterCode;//目标仓库
        $("#workCenterCode").val(workCenterCode);
        workCellCode = api.pageParam.workCellCode;//调入库位
        $("#workCellCode").val(workCellCode);
        $("#boxnum").focus();
        idSelector = "boxnum";
        doSearch();
        searchBoxNum()

        //查询条码解析规则
        initScanRule();


    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            $("#boxnum").blur();
            tmSplit(ret);
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#boxnum").blur();
                tmSplit(tm);
            }
        }
    });
    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        var mrlCode = "";
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            mrlCode = arr[1];
        }
        var qty = "";
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            qty = arr[2];
        }
        var lotCode = "";
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            lotCode = arr[3];
        }
        var tmID = "";
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            tmID = arr[4];
        }
        var boxnum = "";
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            boxnum = arr[9];
        }
        doSHconfig()
        //自动入库
        //saveAuto(sm, mrlCode, lotCode, qty, boxnum, tmID);
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    $("#boxnum").val("");
                    //物料号
                    var mrlCode = map["mrlCode"];
                    //批次号
                    var lotCode = map["lotCode"];
                    //转移数量数量
                    var qty = map["qty"];
                    //条码
                    var tm = map["tm"];
                    //箱号
                    var boxnum = map["boxnum"];
                    //tmID
                    var tmID = map["tmID"];

                    $("#boxnum").val(boxnum);

                    doSHconfig()
                    //自动保存
                    //saveAuto(tm, mrlCode, lotCode, qty, boxnum, tmID);
                } else if (myobj.code == '1') {
                    // $("#boxnum").attr('disabled', false);
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    /*  //箱号监听回车事件
     $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
           if (!clickThrottle()) {
               $("#boxnum").val("");
               return;
           }
           var keycode = e.keyCode;
           var tm = $(this).val();
           $("#boxnum").val("");
           if (keycode == '13') {
               if (tm != null && tm != "" && tm != undefined) {
                   // $("#boxnum").blur();
                   // $("#boxnum").attr('disabled', true);
                   console.log(tm);
                   // $("#boxnum").select();
                   //调用条码解析+保存事件
                   tmSplit(tm);
               }
           }
       }); */
    //查询
    function doSearch() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchPatask";
        var data = {
            values: {
                distributeTaskCode: distributeTaskCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                        html += "<td name='seqno'>" + (i + 1) + "</td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='drawing'>" + list[i].drawing + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='planqty'>" + list[i].countQty + "</td>";
                        html += "<td name='finishqty'>" + list[i].qty + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    function searchBoxNum() {
        var distributeTaskCode = $("#distributeTaskCode").val();
         var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchPataskBox";
        var data = {
            values: {
                distributeTaskCode: distributeTaskCode,
                 un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    arrSave = myobj.list
                    $("#num").val(myobj.list.length)
                    setTable(myobj.list)
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                }
            }
        });
    }


    function setTable(list) {
        var html = "";
        var mrlnum = 0;
        for (var i = 0; i < list.length; i++) {
            // html += "<li class='sortable ui-selectee ui-sortable background-color: #addbef'>";
            // html += "<ul class='aui-list aui-media-list ui-selectee'>";
            // html += "<li class='aui-list-item  ui-selectee'>";
            // html += "<div class='aui-media-list-item-inner ui-selectee'>";
            // html += "<div class='aui-list-item-inner ui-selectee'>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>箱号:</div>";
            // html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].boxCode + "</div>";
            // html += "</div>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料编码:</div>";
            // html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlCode + "</div>";
            // html += "</div>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名称:</div>";
            // html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlName + "</div>";
            // html += "</div>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'>批次号:</div>";
            // html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].lotCode + "</div>";
            // html += "</div>";
            // html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            // html += "<div class='aui-list-item-label ui-selectee list-tr-title'style = 'word-break:break-all'>数量:</div>";
            // html += "<div class='aui-list-item-inner ui-selectee '>" + list[i].qty + "</div>";
            // html += "</div>";
            // html += "</div>";
            // html += "</li>";
            // html += "</ul>";
            // html += "</li>";

            if (list[i].status == "1") {
                html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #32CD32 !important;height: 120px;padding: 0.2rem 0.2rem;font-size: 0.8rem;'>";
                html += "<table style='width:100%;'>";
                html += "<tr style='width:100%;'><td style='width:16%;'>箱号：</td><td  id='boxnumInfo' style='width:70%;' colspan='2'>" + list[i].boxCode + "</td><td></td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>物料号：</td> <td  id='mrlCodeInfo' style='width:70%;' colspan='3'>" + list[i].mrlCode + "</td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>规格</td> <td  id='mrlCodeInfo' style='width:70%;' colspan='3'>" + list[i].drawing + "</td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>批次号：</td><td id='lotCodeInfo' style='width:40%;' >" + list[i].lotCode + "</td> ";
                html += "<td style='width:16%;'>数量：</td><td id='qtyInfo' style='width:20%;'>" + list[i].qty + "</td>";
                html += "</tr>";
                html += "</table></div>";

                mrlnum++
            } else {
                html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 120px;padding: 0.2rem 0.2rem;font-size: 0.8rem;'>";
                html += "<table style='width:100%;'>";
                html += "<tr style='width:100%;'><td style='width:16%;'>箱号：</td><td  id='boxnumInfo' style='width:70%;' colspan='2'>" + list[i].boxCode + "</td><td></td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>物料号：</td> <td  id='mrlCodeInfo' style='width:70%;' colspan='3'>" + list[i].mrlCode + "</td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>规格</td> <td  id='mrlCodeInfo' style='width:70%;' colspan='3'>" + list[i].drawing + "</td></tr>";
                html += "<tr style='width:100%;'><td style='width:20%;'>批次号：</td><td id='lotCodeInfo' style='width:40%;' >" + list[i].lotCode + "</td> ";
                html += "<td style='width:16%;'>数量：</td><td id='qtyInfo' style='width:20%;'>" + list[i].qty + "</td>";
                html += "</tr>";
                html += "</table></div>";
            }

        }
        $("#mrlnum").val(mrlnum)
        $api.html($api.byId("tbodyHtml2"), html);
    };



    function doSHconfig() {
        var distributeTaskCode = $("#distributeTaskCode").val();
        var boxnum = $("#boxnum").val();
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/paAndonSetMrlSave2";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                distributeTaskCode: distributeTaskCode,//配送任务单号
                workCenterCode: workCenterCode,
                workCellCode: workCellCode,
                boxnum: boxnum,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {

                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    searchBoxNum()
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    //清空表单
                    /*  arrSave = [];
                     setTable(arrSave) */

                } else {
                    $("#boxnum").val("")
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    api.hideProgress();
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });

    }



    //订单返回按钮
    function closeWin() {
        api.closeWin({});
        var name = api.pageParam.name;
        api.execScript({
            name: name,
            script: 'search()'
        });
    }
</script>

</html>